package com.cheer.ba.view.activity;

import android.content.Intent;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.HotExchangeModel;
import com.cheer.ba.presenter.impl.HomePresenter;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.dialog.DialogUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 描述 ：送油上门
 * 作者 ：hq
 * 时间 ：2017/5/26 16:10
 */
public class SendOilDoorAty extends BaseCommAty {

    //所选地区
    @BindView(R.id.id_city_name_txt)
    TextView mTxtCity;
    private String mSaveCity;
    private String mProvince;

    //今日油价
    @BindView(R.id.id_today_oil_price_txt)
    TextView mTxtOilPrice;

    //购买金额
    @BindView(R.id.id_buy_money_edit)
    EditText mEditBuyMoney;
    private String mSaveMoney;
    //设备型号
    @BindView(R.id.id_choose_oil_machine_txt)
    TextView mTxtMachine;
    private String mMachine;
    //邮箱容积
    @BindView(R.id.id_oil_rise_edit)
    EditText mEditOilSize;
    private String mOilSize;
    //真实姓名
    @BindView(R.id.id_true_name_edit)
    EditText mEditTrueName;
    private String mTrueName;
    //手机号
    @BindView(R.id.id_person_phone_edit)
    EditText mEditPhone;
    private String mPhone;
    //短信验证码
    @BindView(R.id.id_sms_code_edit)
    EditText mEditCode;
    private String mCode;
    //获取短信验证码
    @BindView(R.id.id_get_code_btn)
    Button mBtnGetCode;
    //送达时间
    @BindView(R.id.id_send_time_txt)
    TextView mTxtSendTime;
    private String mSendTime;
    //省市区
    @BindView(R.id.id_area_txt)
    TextView mTxtArea;
    private String mArea;
    //详细地址
    @BindView(R.id.id_detail_address_location)
    EditText mEditAddress;
    private String mAddress;

    TextView mTxtRight;
    private String mLocation = "选择城市";

    //记录点击的位置 true点击右上角选择城市按钮
    private boolean isClick = true;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_send_oil, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new HomePresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.send_oil_door));
        mSaveCity = "武汉";
        mProvince = "湖北省";
        mTxtCity.setText(mProvince + " #0：");
        mTxtRight = setTitleRight(mLocation, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isClick = true;
                Intent email = new Intent(aty, ProvinceAreaAty.class);
                startActivityForResult(email, IntentCode.Area.PROVINCE_REQUEST_CODE);
            }
        });
        mEditBuyMoney.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }

    @Override
    public void initData() {
        ((HomePresenter) presenter).getTodayOilPrice(mProvince);
        DialogUtils.showCommDialog(aty, "温馨提示，该送油上门服务暂未开放，开放时间请关注加油啦官方网站。", "取消", "知道了", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.DialogDiss();
            }
        });
        DialogUtils.DialogShow();
    }

    /**
     * 检测输入信息的正确性
     *
     * @return
     */
    public boolean checkData() {
        mSaveMoney = mEditBuyMoney.getText().toString().trim();
        mMachine = mTxtMachine.getText().toString().trim();
        mOilSize = mEditOilSize.getText().toString().trim();
        mTrueName = mEditTrueName.getText().toString().trim();
        mPhone = mEditPhone.getText().toString().trim();
        mCode = mEditCode.getText().toString().trim();
        mSendTime = mTxtSendTime.getText().toString().trim();
        mArea = mTxtArea.getText().toString().trim();
        mAddress = mEditAddress.getText().toString().trim();
        if (StringUtils.isEmpty(mSaveMoney) || Double.parseDouble(mSaveMoney) < 500) {
            SVProgressHUD.showInfoWithStatus(aty, getString(R.string.remind_buy_money));
        } else {
            if (StringUtils.isEmpty(mMachine)) {
                SVProgressHUD.showInfoWithStatus(aty, getString(R.string.choose_machine_type));
            } else {
                if (StringUtils.isEmpty(mOilSize)) {
                    SVProgressHUD.showInfoWithStatus(aty, "请输入邮箱容积");
                } else {
                    if (StringUtils.isEmpty(mTrueName)) {
                        SVProgressHUD.showInfoWithStatus(aty, "请输入您的真实姓名");
                    } else {
                        if (StringUtils.isEmpty(mPhone) || !StringUtils.isMobile(mPhone)) {
                            SVProgressHUD.showInfoWithStatus(aty, getString(R.string.phone_error));
                        } else {
                            if (StringUtils.isEmpty(mCode)) {
                                SVProgressHUD.showInfoWithStatus(aty, getString(R.string.input_check_code));
                            } else {
                                if (StringUtils.isEmpty(mSendTime)) {
                                    SVProgressHUD.showInfoWithStatus(aty, "请输入送达时间");
                                } else {
                                    if (StringUtils.isEmpty(mArea)) {
                                        SVProgressHUD.showInfoWithStatus(aty, "请输入配送地址");
                                    } else {
                                        if (StringUtils.isEmpty(mAddress)) {
                                            SVProgressHUD.showInfoWithStatus(aty, "请输入您的详细地址");
                                        } else {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(ConstantTag.HOME_OIL, tag)) {
                List<HotExchangeModel> oils = (List<HotExchangeModel>) t;
                mTxtOilPrice.setText(oils.get(0).p0);
            }
        }
    }

    @OnClick({R.id.id_choose_machine_view, R.id.id_choose_time_view, R.id.id_choose_area_view, R.id.id_oil_submit_btn})
    public void onClick(View mView) {
        switch (mView.getId()) {
            //选择机器型号
            case R.id.id_choose_machine_view:
                break;
            //选择送达时间
            case R.id.id_choose_time_view:
                break;
            //选择送达地址
            case R.id.id_choose_area_view:
                isClick = false;
                Intent email = new Intent(aty, ProvinceAreaAty.class);
                startActivityForResult(email, IntentCode.Area.PROVINCE_REQUEST_CODE);
                break;
            //提交按钮
            case R.id.id_oil_submit_btn:
                if (isLogin(true)) {
                    if (checkData()) {
                        SVProgressHUD.showInfoWithStatus(aty, "暂未开放该服务，如有疑问，请联系客服。");
                    }
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntentCode.Area.PROVINCE_REQUEST_CODE) {
            if (resultCode == IntentCode.Area.PROVINCE_RESULT_CODE) {
                if (null != data) {
                    mArea = data.getStringExtra(IntentKey.General.KEY_DATA);
                    if (isClick) {
                        mLocation = mArea;
                        mTxtRight.setText(mLocation);
                    }
                    mTxtArea.setText(mArea);
                    Session.setUser_area(mArea);
                    String[] s = StringUtils.strSplit(mArea);
                    mArea = s[0] + "|" + s[1] + "|" + s[2];
                    mSaveCity = s[1];
                    mProvince = s[0];
                    mTxtCity.setText(mProvince + " #0：");
                    ((HomePresenter) presenter).getTodayOilPrice(mProvince);
                }
            }
        }
    }
}
